package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Car;
import com.ruoyi.system.service.IExcelService;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

/**
 * 上传excel相关接口
 *
 * @author 蒼飞
 * @date 2023-11-2
 */
@RestController
@RequestMapping("/system/excel")
public class ExcelController extends BaseController {
    @Autowired
    IExcelService iExcelService;
    /**
     * 上传excel
     */
    @PostMapping("/upload")
    public AjaxResult list(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        System.out.println("开始上传");
        InputStream inputStream = multipartFile.getInputStream();
        File tempFile = File.createTempFile("temp", ".tmp");
        try (FileOutputStream out = new FileOutputStream(tempFile)) {
            IOUtils.copy(inputStream, out);
        }
        FileInputStream fileInputStream = new FileInputStream(tempFile);
        inputStream.close();
        //文件输入流读取excel
        return AjaxResult.success(iExcelService.uploadExcel(fileInputStream));
    }
}
